﻿using System;

namespace Web.Extensions
{
    public static class ControlExtensions
    {
        public static void Bind(this System.Web.UI.HtmlControls.HtmlSelect select, object data, string valueField = "Id", string textField = "Nombre")
        {
            select.DataTextField    = textField;
            select.DataValueField   = valueField;
            select.DataSource       = data;
            select.DataBind();
        }

        public static void SetSelected(this System.Web.UI.HtmlControls.HtmlSelect select, object value)
        {
            var item = select.Items.FindByValue(Convert.ToString(value));
            if (item != null)
                item.Selected = true;
        }

        public static void SetValue(this System.Web.UI.HtmlControls.HtmlInputText input, object value)
        {
            input.Value = value.ToString();
        }

        public static void RemoveItems(this System.Web.UI.HtmlControls.HtmlSelect select, int[] values)
        {
            foreach (var value in values)
            {
                var item = select.Items.FindByValue(value.ToString());
                if (item != null)
                {
                    select.Items.Remove(item);
                }
            }            
        }

    }
}